//
//  EleSpeechViewController.m
//  Conference
//
//  Created by chenxin on 12-11-18.
//电子演讲稿
//

#import "EleSpeechViewController.h"
#import "EleSpeakData.h"
#import "EleSpeechCellController.h"
#import "EleSpeechDetailViewController.h"
#import "MainDataBase.h"
#import "NetContant.h"
#import "Globals.h"
#import "WebData.h"
#import "DateTool.h"
#import "Constant.h"
@interface EleSpeechViewController ()

@end

@implementation EleSpeechViewController
@synthesize table;
@synthesize arrayOfData;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self loadingData];
    
    // Do any additional setup after loading the view from its nib.
}
-(void)loadingData{
    [self getDataFromDb];
    NSThread *thread=[[[NSThread alloc] initWithTarget:self selector:@selector(run) object:nil] autorelease];
    [thread start];

}

-(void)getDataFromDb{

    self.arrayOfData=[[MainDataBase getInstance]getEleSpeakData];
}
-(void)run{
    NSAutoreleasePool *pool=[[NSAutoreleasePool alloc] init];
    NSString *sRet=[[WebData getInstance]getEleSpeakInfo:[Globals getInstance].sEleSpeakUpdate];

    [self performSelectorOnMainThread:@selector(onPostExecute:) withObject:sRet waitUntilDone:YES];
    
	[pool release];
    
}

-(void)onPostExecute:(NSString *)sRet{
    
    if ([sRet isEqualToString:[[NetContant getInstance] NET_CONNECT_FALSE]]) {
        
        
      //  [[Globals getInstance] showAlertView:[[Constant getInstance] net_error]];
        
        
    }else if([sRet isEqualToString:[[NetContant getInstance] NETWORK_RECORD_ISEMPTY]]){
        
     //gf   [[Globals getInstance] showAlertView:[[Constant getInstance] account_error]];
        
    }else {
        @try {
            [self doNetRet:sRet];
            [self doNetInfo];
        }
        @catch (NSException *exception) {
            
        }
        
       

    }
    
}
-(void)doNetRet:(NSString *)sRet{
        NSArray *tables=[sRet componentsSeparatedByString:[[NetContant getInstance]TABLES_SPLIT_FLAG]];
        NSMutableArray *dataList=[NSMutableArray arrayWithCapacity:0];
        for (int i=0; i<[tables count]; i++) {
           if(nil!=[tables objectAtIndex:i]){
               NSArray *record=[[tables objectAtIndex:i]componentsSeparatedByString:[[NetContant getInstance] RECORDS_SPLIT_FLAG]];
               if([record count]>=7){
                    EleSpeakData *eleSpeakData=[[EleSpeakData alloc]init];
                    eleSpeakData.ID=[record objectAtIndex:0];
                    eleSpeakData.title = [record objectAtIndex:1];
                    eleSpeakData.message = [record objectAtIndex:2];
                    eleSpeakData.speak=[record objectAtIndex:3];
                    eleSpeakData.dateTime=[record objectAtIndex:4];
                    eleSpeakData.headUrl=[record objectAtIndex:5];
                    eleSpeakData.arrImg=[record objectAtIndex:6];
                    
                    [dataList addObject:eleSpeakData];
                   [eleSpeakData release];
                }
            }
        }
    [[MainDataBase getInstance] addEleSpeak:dataList];
    [Globals getInstance].sEleSpeakUpdate=[NSString stringWithFormat:@"%@ %@",[[DateTool getInstance]getNowDate],[[DateTool getInstance]getNowTime]];
    
}
-(void)doNetInfo{
    [self getDataFromDb];
    [self.table reloadData];
    
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [self tableView:tableView cellForRowAtIndexPath:indexPath];
    return cell.frame.size.height;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    //    return 0;
    return [self.arrayOfData count];
}

-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *eleSpeechCell=@"eleSpeechCell";
    EleSpeechCellController *cell=(EleSpeechCellController *)[tableView dequeueReusableCellWithIdentifier:eleSpeechCell];
    if(cell ==nil){
		NSArray *array = [[NSBundle mainBundle] loadNibNamed:@"EleSpeechCellController" owner:self options:nil];
		for (id oneObject in array) {
			if ([oneObject isKindOfClass:[EleSpeechCellController class]]) {
				cell=oneObject;
				break;
			}
		}
        cell.selectionStyle=UITableViewCellSelectionStyleBlue;
    }
    EleSpeakData  *data=[self.arrayOfData objectAtIndex:[indexPath row]];
    NSString *headUrl=data.headUrl;
    if(nil!=headUrl&&![@"" isEqualToString:headUrl]){}
    cell.lab_title.text=data.title;
    cell.lab_name.text=data.speak;
    cell.lab_time.text=data.dateTime;
    
    if([indexPath row] == self.arrayOfData.count-1)
        [cell.img_split_line setHidden:YES];
    else {
        [cell.img_split_line setHidden:NO];
    }

    return cell;
    
}

-(void)tableView:(UITableView *)tableView1 didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    EleSpeechDetailViewController *mEleSpeechDetailViewController=[[EleSpeechDetailViewController alloc]initWithNibName:@"EleSpeechDetailViewController" bundle:nil];
    mEleSpeechDetailViewController.eleSpeakData=[self.arrayOfData objectAtIndex:[indexPath row]];
    [self.navigationController pushViewController:mEleSpeechDetailViewController animated:YES];
    [mEleSpeechDetailViewController release];
     [tableView1 deselectRowAtIndexPath:indexPath animated:YES];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)dealloc {
    [table release];
    [super dealloc];
}
- (void)viewDidUnload {
    [self setTable:nil];
    [super viewDidUnload];
}
@end
